import request from '@/utils/request'
import type { PageResponse } from '@/types'

export interface Category {
  id: number
  name: string
  code: string
  description?: string
  subjectId: number
  subjectName?: string
  status: 'ACTIVE' | 'INACTIVE'
  createdAt: string
  updatedAt: string
}

export interface CategoryQuery {
  page: number
  size: number
  name?: string
  code?: string
  subjectId?: number
  status?: string
}

/**
 * 学科分类API接口
 */
export const categoryApi = {
  /**
   * 获取分类列表
   * @param params 查询参数
   */
  getCategories: (params: CategoryQuery) =>
    request.get<PageResponse<Category>>('/categories', { params }),

  /**
   * 获取分类详情
   * @param id 分类ID
   */
  getCategory: (id: number) =>
    request.get<Category>(`/categories/${id}`),

  /**
   * 创建分类
   * @param data 分类数据
   */
  createCategory: (data: Omit<Category, 'id' | 'createdAt' | 'updatedAt' | 'subjectName'>) =>
    request.post<Category>('/categories', data),

  /**
   * 更新分类
   * @param id 分类ID
   * @param data 分类数据
   */
  updateCategory: (id: number, data: Partial<Category>) =>
    request.put<Category>(`/categories/${id}`, data),

  /**
   * 删除分类
   * @param id 分类ID
   */
  deleteCategory: (id: number) =>
    request.delete(`/categories/${id}`),

  /**
   * 根据学科ID获取分类列表
   * @param subjectId 学科ID
   */
  getCategoriesBySubject: (subjectId: number) =>
    request.get<Category[]>(`/categories/by-subject/${subjectId}`)
}